package com.jprocms.module.cms.controller.portal.page;

import com.jprocms.module.cms.dal.dataobject.guestbook.GuestbookDO;
import com.jprocms.module.cms.dal.dataobject.site.SiteDO;
import com.jprocms.module.cms.enums.ErrorCodePortalConstants;
import com.jprocms.module.cms.exception.PortalServiceExceptionUtil;
import com.jprocms.module.cms.service.content.ContentCountService;
import com.jprocms.module.cms.service.guestbook.GuestbookService;
import com.jprocms.module.cms.service.site.SiteService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 留言板 Controller
 *
 * @author jprocms
 */
@Controller("portalGuestbookController")
public class GuestbookController {

    private static final String TPL_GUESTBOOK = "sys/guestbook";
    private static final String TPL_GUESTBOOK_POST = "sys/guestbook_post";
    private static final String TPL_GUESTBOOK_DETAIL = "sys/guestbook_detail";
    private final SiteService siteService;
    private final GuestbookService service;

    private final ContentCountService contentCountService;

    public GuestbookController(SiteService siteService, GuestbookService service, ContentCountService contentCountService) {
        this.siteService = siteService;
        this.service = service;
        this.contentCountService = contentCountService;
    }

    @GetMapping({"/guestbook", "/{sitePath:[\\w-]+}/guestbook"})
    public String guestbookIndex(@PathVariable(required = false) String sitePath, HttpServletRequest request) {
        SiteDO site = siteService.getSite(sitePath);
        validate(site);
        contentCountService.viewIndex(site.getId());
        return site.joinTemplate(TPL_GUESTBOOK);
    }

    @GetMapping({"/guestbook/post", "/{sitePath:[\\w-]+}/guestbook/post"})
    public String create(@PathVariable(required = false) String sitePath) {
        SiteDO site = siteService.getSite(sitePath);
        validate(site);
        contentCountService.viewIndex(site.getId());
        return site.joinTemplate(TPL_GUESTBOOK_POST);
    }

    @GetMapping({"/guestbook/{id}", "/{sitePath:[\\w-]+}/guestbook/{id}"})
    public String getDetail(@PathVariable(required = false) String sitePath, @PathVariable Long id, Map<String, Object> modelMap) {
        SiteDO site = siteService.getSite(sitePath);
        validate(site);
        GuestbookDO guestbook = service.getGuestbook(id);
        if (guestbook == null) {
            throw PortalServiceExceptionUtil.exception(ErrorCodePortalConstants.DATA_NOT_EXIST);
        }
        modelMap.put("guestbook", guestbook);
        contentCountService.viewIndex(site.getId());
        return site.joinTemplate(TPL_GUESTBOOK_DETAIL);
    }

    private void validate(SiteDO site) {
        if (!site.getSiteConfig().getGuestbook().getIsOpen()) {
            throw PortalServiceExceptionUtil.exception(ErrorCodePortalConstants.GUESTBOOK_NOT_ENABLE);
        }
    }

}
